#pragma once
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>
#include <QMatrix4x4>
#include <QPoint>

class MatrixRenderWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core {
    Q_OBJECT
public:
    explicit MatrixRenderWidget(QWidget *parent = nullptr);
    ~MatrixRenderWidget() override;

    void showVectorSum(const QPointF &v1, const QPointF &v2);
    void showMatrixTransform(const QVector4D &m);

protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

    // 交互事件…
    void mousePressEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev) override;
    void wheelEvent(QWheelEvent *ev) override;
    void keyPressEvent(QKeyEvent *ev) override;

private:
    // 静态部分
    void initShaders();
    void initGeometry();
    void drawAxisArrows();
    void drawArrow();

    // 动态绘制部分
    void drawSumVectors();
    void drawMatrixVectors();

    void updateMatrices();

    QOpenGLVertexArrayObject vao;
    QOpenGLBuffer vbo;
    QOpenGLShaderProgram shaderProgram;

    QMatrix4x4 projection, view, model;
    float panX = 0, panY = 0, scaleFactor = 1;

    // 用户数据
    QPointF vector1_, vector2_;
    QVector4D matrix_;
    bool displaySum_    = false;
    bool displayMatrix_ = false;
    QPoint lastPos;
};
